
//
// 101101 - AH - Created.
// 101128 - AH - Moved from package org.soekrat.engine.mine
//

package org.soekrat.engine.io;

import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

/**
 * <p>
 *   Default
 *   {@link ResourceFactory} implementation.
 * </p>
 * @author Arne Halvorsen (AH) 
 * @author Laetitia Le Chatton (LLC) 
 */
public final class DefaultResourceFactory implements ResourceFactory
{
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public DefaultResourceFactory(){}

  // Specified in ResourceFactory
  public Object create(String spec) throws IOException
  {
    if (spec==null)
    {
      throw new NullPointerException("spec");
    }
    
    int idx=spec.indexOf(":");
    if (idx!=-1)
    {
      String type=spec.substring(0, idx);
      String r=spec.substring(idx+1, spec.length());      
           if (type.equals("txt"))   return r;
      else if (type.equals("image")) return image(r);
    }
    
    throw new IllegalArgumentException("spec : "+spec); 
  }
  
  private static Object image(String r) throws IOException
  {
    return ImageIO.read(new URL(r));
  }
  
}
